﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace EmailMarketingCampaign
{
    public partial class TypeofMessage : System.Web.UI.Page
    {
        Message m = new Message();
        MessageControl mc = new MessageControl();
        CampaignControl cc = new CampaignControl();
        static int EmailID;
        //static string temName;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["EmailID"] != null)
            {
                EmailID = int.Parse(Request.QueryString["EmailID"]);


            }
            //if (Request.QueryString["temName"] != null)
            //{
            //    temName = Request.QueryString["temName"].ToString ();
            //    txtTemplate.Text = temName;


            //}

            if (!Page.IsPostBack)
            {
                if (Session["campaign"] != null)
                {

                    if (((Campaign)Session["campaign"]).getEmailID().ToString() != "0")
                    {
                        txtMessage.Text = mc.getSubjectByCampaignID(int.Parse(((Campaign)Session["campaign"]).getCampaignID().ToString()));
                        rbMessage.Checked = true;
                        btnMessagelist.Enabled = true;
                    }
                    else
                    {

                        int a = cc.getCampaignIDbyContent(((Campaign)Session["campaign"]));
                        ((Campaign)Session["campaign"]).setCampaignID(a);

                    }
                }

                if (((Message)Session["message"]) != null)
                {
                    rbMessage.Checked = true;
                    txtMessage.Text = ((Message)Session["message"]).getSubject().ToString();
                    btnMessagelist.Enabled = true;
                    txtTemplate.Text = "";

                }
                if (Session["temName"] != null)
                {
                    txtTemplate.Text = Session["temName"].ToString();
                    Rbtemplate.Checked = true;
                    btnTempleList.Enabled = true;
                    txtMessage.Text = "";
                }



            }

        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Session["temName"] = null;
            Response.Redirect("CreateCamp.aspx");
        }

        protected void btnMessagelist_Click(object sender, EventArgs e)
        {
            Session["temName"] = null;
            Response.Redirect("MailList.aspx");
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            string subject = "";
            string templeName = "";
            if (((Campaign)Session["campaign"]).getEmailID().ToString() != "0")
            {
                if (rbNew.Checked == true)
                {
                    ((Message)Session["message"]).setSubject("");
                    ((Message)Session["message"]).setBody("");

                }
                else if (rbMessage.Checked == true)
                {
                    subject = txtMessage.Text.ToString();
                    string body = mc.getMessageBodyByEmailID(EmailID);
                    ((Message)Session["message"]).setSubject(subject);
                    ((Message)Session["message"]).setBody(body);
                }
                else
                {
                    templeName = txtTemplate.Text.ToString();
                    string body = mc.getMessageBodyByTempleName(templeName).ToString();
                    ((Message)Session["message"]).setBody(body);
                    ((Message)Session["message"]).setSubject("");
                }

                cc.updateCampaignByCampaignID(((Message)Session["message"]), ((Campaign)Session["campaign"]));
            }

            else
            {

                if (rbNew.Checked == true)
                {
                    Session["message"] = m;
                    m.setSubject("");
                    m.setBody("");

                    if (mc.exitCampainInEmail(((Campaign)Session["campaign"]).getCampaignID()))
                    {
                        cc.updateCampaignByCampaignID(((Message)Session["message"]), ((Campaign)Session["campaign"]));

                    }
                    else

                        mc.insertNewMessage(m, (Campaign)Session["campaign"]);
                }

                else if (rbMessage.Checked == true)
                {

                    subject = txtMessage.Text.ToString();
                    Session["message"] = m;
                    m.setSubject("");

                    string body = mc.getMessageBodyByEmailID(EmailID);
                    m.setBody(body);



                    if (mc.exitCampainInEmail(((Campaign)Session["campaign"]).getCampaignID()))
                    {
                        cc.updateCampaignByCampaignID(((Message)Session["message"]), ((Campaign)Session["campaign"]));

                    }
                    else

                        mc.insertNewMessage(m, (Campaign)Session["campaign"]);


                }
                else
                {
                    templeName = txtTemplate.Text.ToString();
                    string body = mc.getMessageBodyByTempleName(templeName);
                    Session["message"] = m;
                    m.setBody(body);



                    if (mc.exitCampainInEmail(((Campaign)Session["campaign"]).getCampaignID()))
                    {
                        cc.updateCampaignByCampaignID(((Message)Session["message"]), ((Campaign)Session["campaign"]));

                    }
                    else

                        mc.insertNewMessageTemple(m, (Campaign)Session["campaign"]);


                }
            }

            Response.Redirect("EditMessage.aspx");
        }

        protected void btnTempleList_Click(object sender, EventArgs e)
        {
            Session["temName"] = null;
            Response.Redirect("TempleList.aspx");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            {
                string subject = "";
                string templeName = "";

                if (((Campaign)Session["campaign"]).getEmailID().ToString() != "0")
                {
                    if (rbNew.Checked == true)
                    {
                        ((Message)Session["message"]).setSubject("");
                        ((Message)Session["message"]).setBody("");
                        Session["temName"] = null;


                    }
                    else if (rbMessage.Checked == true)
                    {
                        subject = txtMessage.Text.ToString();
                        ((Message)Session["message"]).setSubject(subject);
                        string body = mc.getMessageBodyByEmailID(EmailID);
                        ((Message)Session["message"]).setBody(body);
                        Session["temName"] = null;

                    }
                    else
                    {
                        templeName = txtTemplate.Text;
                        string body = mc.getMessageBodyByTempleName(templeName);
                        ((Message)Session["message"]).setBody(body);
                        ((Message)Session["message"]).setSubject("");

                    }


                    cc.updateCampaignByCampaignID(((Message)Session["message"]), ((Campaign)Session["campaign"]));

                }
                else
                {
                    if (rbNew.Checked == true)
                    {

                        Session["message"] = m;
                        m.setSubject("");
                        m.setBody("");
                        Session["temName"] = null;


                        if (mc.exitCampainInEmail(((Campaign)Session["campaign"]).getCampaignID()))
                        {
                            cc.updateCampaignByCampaignID(((Message)Session["message"]), ((Campaign)Session["campaign"]));

                        }
                        else

                            mc.insertNewMessage(m, (Campaign)Session["campaign"]);
                    }

                    else if (rbMessage.Checked == true)
                    {
                        Session["temName"] = null;
                        subject = txtMessage.Text.ToString();
                        Session["message"] = m;
                        m.setSubject(subject);
                        string body = mc.getMessageBodyByEmailID(EmailID);
                        if (mc.exitCampainInEmail(((Campaign)Session["campaign"]).getCampaignID()))
                        {
                            cc.updateCampaignByCampaignID(((Message)Session["message"]), ((Campaign)Session["campaign"]));

                        }
                        else

                            mc.insertNewMessage(m, (Campaign)Session["campaign"]);
                    }
                    else
                    {
                        templeName = txtTemplate.Text;
                        string body = mc.getMessageBodyByTempleName(templeName);
                        Session["message"] = m;
                        m.setBody(body);
                        m.setSubject(subject);


                        if (mc.exitCampainInEmail(((Campaign)Session["campaign"]).getCampaignID()))
                        {
                            cc.updateCampaignByCampaignID(((Message)Session["message"]), ((Campaign)Session["campaign"]));

                        }
                        else

                            mc.insertNewMessageTemple(m, (Campaign)Session["campaign"]);


                    }



                }


                this.Response.Write(" <script>alert('save sucseeful.'); </script>");
            }
        }

        protected void rbMessage_CheckedChanged(object sender, EventArgs e)
        {
            txtMessage.Enabled = true;
            btnMessagelist.Enabled = true;
            btnTempleList.Enabled = false;
            txtTemplate.Enabled = false;
            txtTemplate.Text = "";
        }

        protected void Rbtemplate_CheckedChanged(object sender, EventArgs e)
        {
            txtTemplate.Enabled = true;
            btnTempleList.Enabled = true;
            txtMessage.Enabled = false;
            btnMessagelist.Enabled = false;
            txtMessage.Text = "";
        }

        protected void rbNew_CheckedChanged(object sender, EventArgs e)
        {
            txtMessage.Enabled = false;
            btnMessagelist.Enabled = false;
            btnTempleList.Enabled = false;
            txtTemplate.Enabled = false;
            txtTemplate.Text = "";
            txtMessage.Text = "";
        }
    }
}
